<template>
  <div style="padding: 10px">

      <div style="margin: 10px 0">
          <el-button type="info" style="margin-left: 5px; margin-right: 5px" @click="wordsEdit">敏感词设置</el-button>
          <el-input v-model="search" placeholder="请输入关键字" style="width: 20%" clearable></el-input>
          <el-button type="primary" style="margin-left: 5px" @click="load">查询</el-button>
          <el-button type="warning" style="margin-left: 5px" @click="searchByWords">敏感词查询</el-button>
      </div>

    <el-table
            v-loading="MessageLoading"
            :data="tableData"
            border
            stripe
            style="width: 100%">
      <el-table-column
              prop="id"
              label="ID"
              sortable
      >
      </el-table-column>
      <el-table-column
              prop="content"
              label="留言内容">
      </el-table-column>
      <el-table-column
              prop="username"
              label="留言账号">
      </el-table-column>
      <el-table-column
              prop="time"
              label="留言时间">
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-popconfirm title="确定删除吗？" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
          <el-popconfirm title="确定封禁次账号吗？" @confirm="ban(scope.row.username)">
            <template #reference>
              <el-button size="mini" type="danger">封禁</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin: 10px 0">
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>
    </div>

    <el-dialog title="敏感词设置" v-model="dialogFormVisible" width="30%" draggable="true">
      <el-table
              v-loading="WordsLoading"
              :data="wordsData"
              border
              stripe
              style="width: 100%">
        <el-table-column
                prop="id"
                label="ID"
                sortable
                width="100xp"
        >
        </el-table-column>
        <el-table-column
                prop="word"
                label="敏感词">
        </el-table-column>
        <el-table-column label="操作" width="100xp">
          <template #default="scope">
            <el-popconfirm title="确定删除吗？" @confirm="handleDeleteW(scope.row.id)">
              <template #reference>
                <el-button size="mini" type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>

      <div style="margin: 10px 0">
        <el-pagination
                @size-change="handleSizeChangeW"
                @current-change="handleCurrentChangeW"
                :current-page="currentPageW"
                :page-sizes="[5, 10, 20]"
                :page-size="pageSizeW"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalW">
        </el-pagination>
      </div>

      <template #footer>
        <el-form :model="form" label-width="120px">
          <el-form-item prop="words">
            <el-input v-model="form.word" style="width: 40%" placeholder="新增敏感词" clearable></el-input>
            <el-button type="primary" @click="wordsAdd" style="margin-left: 10px">新 增</el-button>
            <el-button type="primary" @click="cancel">应 用</el-button>
          </el-form-item>
        </el-form>

      </template>
    </el-dialog>
  </div>
</template>

<script>

  import request from "@/utils/request";

export default {
  name: "MessageMG",
  data() {
    return {
      MessageLoading: false,
      WordsLoading: false,
      search: '',
      currentPage: 1,
      currentPageW: 1,
      pageSize: 10,
      pageSizeW: 5,
      total: 0,
      totalW: 0,
      tableData: [],
      wordsData: [],
      form: {},
      dialogFormVisible: false,
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.MessageLoading = true;
      request.get("/message/page", {
        params: {
          name: this.search,
          pageNum: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(res => {
        this.MessageLoading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    searchByWords() {
      this.MessageLoading = true;
      request.get("/message/getByWords", {
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize
        }
      }).then(res => {
        this.MessageLoading = false
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    loadWords() {
      this.WordsLoading = true
      request.get("/words/page", {
        params: {
          pageNum: this.currentPageW,
          pageSize: this.pageSizeW
        }
      }).then(res => {
        this.WordsLoading = false
        this.wordsData = res.data.records
        this.totalW = res.data.total
      })
    },
    wordsAdd() {
      if (!this.form.word){
        this.$message.warning("请输入敏感词!")
        return
      }
      request.post("/words", this.form).then( res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "新增成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
      this.form = {}
      this.loadWords()
    },
    wordsEdit() {
      this.dialogFormVisible = true
      this.loadWords()
    },
    cancel() {
      this.dialogFormVisible = false;
      this.load()
    },
    ban(username) {
      request.get("/user/ban", {
        params: {
          username: username,
          available: false
        }
      }).then(res => {
        if (res.code === '0') {
          this.$message.success("封禁成功！")
        } else {
          this.$message.error(res.msg)
        }
      })
      /*this.$message.warning("功能尚未完善！")*/
    },
    handleDelete(id) {
      console.log(id)
      request.delete("/message/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.load()  // 删除之后重新加载表格的数据
      })
    },
    handleDeleteW(id) {
      request.delete("/words/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: "删除成功"
          })
        } else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
        this.loadWords()  // 删除之后重新加载表格的数据
      })
    },
    handleSizeChange(pageSize) {   // 改变当前每页的个数触发
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {  // 改变当前页码触发
      this.currentPage = pageNum
      this.load()
    },
    handleSizeChangeW(pageSize) {   // 改变当前每页的个数触发
      this.pageSizeW = pageSize
      this.loadWords()
    },
    handleCurrentChangeW(pageNum) {  // 改变当前页码触发
      this.currentPageW = pageNum
      this.loadWords()
    }
  }
}
</script>

<style scoped>

</style>
